package gohttp

import (
	"net"
	"net/http"
)

type GoHttpServ struct {
	http.Server
	OnListenSucc func(ls net.Listener)
}

func NewGoHttpServ(addr string) *GoHttpServ {
	rval := &GoHttpServ{}
	rval.Addr = addr
	rval.Handler = nil
	return rval
}

func (this *GoHttpServ) ServeHttp(network string) error {
	ln, err := net.Listen(network, this.Addr)
	if err != nil {
		return err
	}
	evt := this.OnListenSucc
	if evt != nil {
		evt(ln)
	}
	return this.Serve(ln)
}

func RunDefault(addr string) error {
	def := NewGoHttpServ(addr)
	return def.ServeHttp("tcp4")
}
